<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('category_form.class.php');
require_once('category_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/category/tcms_category_dbc.class.php');


class Category_Form_Ctrl extends GForm_Ctrl_With_Object {
	private $parent = false;

	function __construct($system_name, Vars_Manager $vm, DB_Connection $dbc, $object_id_capture = 'obj_id') {
		parent::__construct($system_name, $vm, $dbc, $object_id_capture);
	}

	public function set_parent($parent) {
		$this->parent = $parent;
	}


	protected function create_form() {
		$form = new Category_Form();

//		$form->generate_html();

		return $form;
	}


	protected function create_form_view() {
		$ret = new Category_Form_View($this->form);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		if (!ereg("^[a-z]{1}[a-z0-9_]{0,100}$" , $this->form->get_field_value('hid'))) {
			$this->form->set_field_error('hid', 'invalid_hid');
			$ret = false;
		} else {
			$dbc = $this->get_dbc();
			$object = $this->get_object();
			if ($object->get_id()) {
				if (!TCMS_Category_DBC::is_unique_hid_update($dbc, $this->form->get_field_value('hid'), $object->get_id())) {
					$this->form->set_field_error('hid', 'not_unique');
					$ret = false;
				}
			} else {
				if (!TCMS_Category_DBC::is_unique_hid($dbc, $this->form->get_field_value('hid'))) {
					$this->form->set_field_error('hid', 'not_unique');
					$ret = false;
				}
			}
		}

		return $ret;
	}


	protected function on_bad_submit() {
		$this->export('parent_parent_category', $this->get_permanent_external_param('parent_parent_category'));

//		printbr('lajna');
	}


	protected function create_object() {
		$object = new TCMS_Category_DBC();

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('title', $object->get_title());
		if ($this->parent !== false) {
			$form->set_field_value('parent', $this->parent);
		} else {
			$form->set_field_value('parent', $object->get_parent());
		}
		$form->set_field_value('hid', $object->get_hid());
		$form->set_field_value('ordering', $object->get_ordering());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_title($form->get_field_value('title'));
		$object->set_parent($form->get_field_value('parent'));
		$object->set_hid($form->get_field_value('hid'));
		if ($form->get_field_value('ordering')) {
			$object->set_ordering($form->get_field_value('ordering'));
		} else {
			$object->set_ordering(10);
		}
	}


	public function prepare_order($parent) {
		$dbc = $this->get_dbc();

		$next_ordering = TCMS_Category_DBC::get_next_order($dbc, $parent);

		$form = $this->get_form();
		$object = $this->get_object();
		$object->set_ordering($next_ordering);
	}
}